/**
 * 聚合支付mixins
 */
import PayType, { PAYMENTSCENARIO } from "@/utils/enum/order/PayType";
import platfrom from "@/utils/platform";
import { weixinMIniProgramPay, weixinH5pay } from "@/utils/pay/index";
import util from "@/utils/util.js";
import request from "@/utils/require";

const WEIXINJSSDKURL = "http://res.wx.qq.com/open/js/jweixin-1.6.0.js";
let app;
const aggregatePayMixins = {
  data() {
    return {
      payTypes: [],
      paytype: "",
    };
  },
  computed: {},
  created() {
    app = getApp();
  },
  mounted() {},
  methods: {
    // 获取支付方式
    getPaySetting() {
      const web_id = uni.getStorageSync("web_id");
      request
        .post(`index.php?s=/api/page/getPaySetting&wxapp_id=${web_id}`)
        .then((res) => {
          if (res.data.code === 1) {
            const datas = Object.values(res.data.data);
            this.payTypes = datas.filter(
              (e) => !!Number(e.is_open) && !!Number(e.platfrom[platfrom])
            );
            this.paytype = this.payTypes.find((e) => e.is_open).value;
          }
        });
    },
	getBalancePaySetting() {
	  const web_id = uni.getStorageSync("web_id");
	  request
	    .post(`index.php?s=/api/page/getBalancePaySetting&wxapp_id=${web_id}`)
	    .then((res) => {
	      if (res.data.code === 1) {
	        const datas = Object.values(res.data.data);
	        this.payTypes = datas.filter(
	          (e) => !!Number(e.is_open) && !!Number(e.platfrom[platfrom])
	        );
	        this.paytype = this.payTypes.find((e) => e.is_open).value;
	      }
	    });
	},
    paytypeChange(e) {
      this.paytype = e.currentTarget.dataset.type
    },
    getPayOption(data) {
      return new Promise((resolve, reject) => {
        uni.showLoading({
          title: "...",
        });
        request
          .post(
            `index.php?s=/api/package/newdoPay&wxapp_id=` +
              uni.getStorageSync("web_id"),
            data
          )
          .then((res) => {
            if (res.data.code === 1) {
              resolve(res.data);
            } else {
              uni.showToast({
                title: res.data.msg,
                icon: "none",
              });
            }
          })
          .catch(reject);
      });
    },
    getTopUpOption(data) {
      return new Promise((resolve, reject) => {
        uni.showLoading({
          title: "...",
          // title: "正在处理...",
        });
        request
          .post(
            `index.php?s=/api/recharge/newRechargesubmit&wxapp_id=` +
              uni.getStorageSync("web_id"),
            data
          )
          .then((res) => {
            if (res.data.code === 1) {
              resolve(res.data);
            } else {
              uni.showToast({
                title: res.data.msg,
                icon: "none",
              });
            }
          })
          .catch(reject);
      });
    },
	getGradeOption(data) {
	  return new Promise((resolve, reject) => {
	    uni.showLoading({
	      title: "...",
	      // title: "正在处理...",
	    });
	    request
	      .post(
	        `index.php?s=/api/grade/buyGradesubmit&wxapp_id=` +
	          uni.getStorageSync("web_id"),
	        data
	      )
	      .then((res) => {
	        if (res.data.code === 1) {
	          resolve(res.data);
	        } else {
	          uni.showToast({
	            title: res.data.msg,
	            icon: "none",
	          });
	        }
	      })
	      .catch(reject);
	  });
	},
    /**
     * @method 聚合支付入口
     * @param token
     * @param id 订单id
     * @param paytype 支付类型
     * @param coupon_id 优惠券id
     * @return Promise()
     */
    async pay(data, paymentScenario = PAYMENTSCENARIO.PAY) {
      const { paytype } = data;
      let res;
      const options = {
        ...data,
        client: platfrom,
      };
      if (paymentScenario == PAYMENTSCENARIO.PAY) {
        res = await this.getPayOption(options);
      } else if (paymentScenario == PAYMENTSCENARIO.TOPUP) {
        res = await this.getTopUpOption(options);
      }else if (paymentScenario == PAYMENTSCENARIO.GRADE) {
        res = await this.getGradeOption(options);
      }

      const { payment } = res.data;
      if (paytype === PayType.WECHAT.value) {
        // 选择微信支付
        if (platfrom === "MP-WEIXIN") {
          // 小程序
          return weixinMIniProgramPay(payment);
        } else if (platfrom === "H5-WEIXIN") {
          // 公众号
          await util.importScript(WEIXINJSSDKURL);
          return weixinH5pay(payment);
        } else {
          // H5
          window.location.href = res.data.url;
        }
      } else if (paytype == PayType.BALANCE.value) {
        // 选择余额支付
        return Promise.resolve();
      } else if(paytype == PayType.OMIPAY.value){
		  // 选择OMIPAY支付
		  if (platfrom === "MP-WEIXIN") {
		    // 小程序
		    return weixinMIniProgramPay(payment);
		  } else if (platfrom === "H5-WEIXIN") {
		    // 公众号
		    await util.importScript(WEIXINJSSDKURL);
		    return weixinH5pay(payment);
		  } else {
		    // H5
		    window.location.href = res.data.url;
		  }
	  }
    },
  },
};

export default aggregatePayMixins;
